#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct sl {
	struct sl* next;
	int num;
}sl;
void Init(sl* psl) {
	assert(psl);
	psl->next = NULL;
	psl->num = 0;
}
void PushBack(sl* psl) {
	assert(psl);
	if (psl->next == NULL)
	{
		psl->next = (sl*)malloc(sizeof(sl));
		psl->next->next = psl->next;
		psl->next->num = 1;
	}
	else {
		sl* tmp = psl->next;
		sl* newnode = (sl*)malloc(sizeof(sl));
		while (tmp->next != psl->next)
		{
			tmp = tmp->next;
		}
		newnode->next = tmp->next;
		newnode->num = tmp->num + 1;
		tmp->next = newnode;
	}
}
void Pop(sl* psl, sl* node) {
	assert(psl);
	assert(node);
	assert(psl->next);
	sl* tmp = psl;
	if (psl->next == node)
	{
		psl->next = node->next;
	}
	while (tmp->next != node)
	{
		tmp = tmp->next;
	}
	tmp->next = node->next;
	free(node);
}
int main() {
	sl psl;
	Init(&psl);
	int n, m;
	scanf("%d %d", &n, &m);
	for (int s = 0; s < n; s++)
	{
		PushBack(&psl);
	}
	sl* tmp = &psl;
 	while (tmp->next != tmp->next->next)
	{
			for (int p = 0; p < m; p++)
			{
				tmp = tmp->next;
			}
		sl* tmp2 = tmp->next;
		Pop(&psl, tmp);
		tmp = tmp2;
	}
	printf("%d", tmp->num);
	free(psl.next);
}
